package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.IUserService;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;
import com.xxxx.yoyo.service.impl.UserServiceImpl;
import com.xxxx.yoyo.util.SafeUtil;

/**
 * 个人中心类
 * Servlet implementation class MyCenterServlet
 */
@WebServlet("/index/my")
public class MyCenterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ITypeService typeService = new TypeServiceImpl();
	private IUserService userService = new UserServiceImpl();
	

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取session
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user == null) {//用户没有登录或者是session超时失效
			//重定向到登录页面
			response.sendRedirect("/YoyoShop/index/login");
		}else {
			//设置导航条上的 [个人中心] 处于选中状态
			request.setAttribute("flag", 4);
			
			//获取 [宠物分类] 列表
			List<Type> typeList = typeService.getTypeList();
			//把typeList存入request中,供/index/my.jsp页面使用
			request.setAttribute("typeList", typeList);
			
			//请求转发,跳转到/index/my.jsp
			request.getRequestDispatcher("/index/my.jsp").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取session
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		
		if(user == null) {//说明session超时失效
			//重定向到登录页面
			response.sendRedirect("/YoyoShop/index/login");
		}else {//session没有超时
			//设置编码格式为UTF-8    ----解决中文参数乱码
			request.setCharacterEncoding("UTF-8");
			
			//获取前端提交的参数(id, type)
			String userIdStr = request.getParameter("id");
			Integer userId = Integer.parseInt(userIdStr);
			
			String typeStr = request.getParameter("type");
			Integer type = Integer.parseInt(typeStr);
			
			if(type == 1) {//修改个人信息
				//获取前端提交的参数name,phone,address
				String name = request.getParameter("name");
				String phone = request.getParameter("phone");
				String address = request.getParameter("address");
				
				//根据用户id获取用户信息
				User findedUser = userService.getUserById(userId);
				//更新用户信息（先把用户信息放入列表中，再修改数据库）
				findedUser.setName(name);
				findedUser.setPhone(phone);
				findedUser.setAddress(address);
				
				//更新数据库中用户的信息
				boolean isSuccess = userService.updateUser(findedUser);
				if(isSuccess) {//修改成功
					//提示成功
					request.setAttribute("msg", "个人信息修改成功!");
					
					//更新session中的user
					session.setAttribute("user", findedUser);
					
				}else {//修改失败
					//提示失败
					request.setAttribute("msg", "个人信息修改失败!姓名或者家庭住址长度过长!");
				}
			}else if(type == 2) {//修改密码
				//获取前端提交的参数password,passwordNew
				String password = request.getParameter("password");//原密码
				String passwordNew = request.getParameter("passwordNew");//新密码
				//根据用户id获取用户信息
				User findedUser = userService.getUserById(userId);
				//获取要修改的用户的原密码(加密后的)
				String currentPwd = findedUser.getPassword();
				
				//对用户输入的原密码进行加密
				String oldPwd = SafeUtil.encode(password);
				
				//判断用户输入的原密码和查到的用户的原密码是否相同
				if(currentPwd.equals(oldPwd)) {//如果相同,才去修改密码
					//更新用户密码
					findedUser.setPassword(SafeUtil.encode(passwordNew));
					//更新用户信息(更新数据库)
					userService.updateUser(findedUser);
					
					//更新session中的user
					session.setAttribute("user", findedUser);
					
					//提示成功
					request.setAttribute("msg", "密码修改成功!");
				}else {//如果不同,提示错误
					//提示错误
					request.setAttribute("msg", "密码修改失败!原密码输入有误!");
				}
			}
			
			//设置导航条上的 [个人中心] 处于选中状态
			request.setAttribute("flag", 4);
			
			//获取 [宠物分类] 列表
			List<Type> typeList = typeService.getTypeList();
			//把typeList存入request中,供/index/my.jsp页面使用
			request.setAttribute("typeList", typeList);
			
			//请求转发,回到/index/my.jsp页面
			request.getRequestDispatcher("/index/my.jsp").forward(request, response);

		}
		
	}

}
